/**
  ******************************************************************************
  * File Name          : envirctrl.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "network.h"
#include "route.h"

static Network_JsonPointerType jsp;
static char valuebuf[128];

static uint8_t route_dk_up_state(Route_PayloadType *rtpl)
{
	Ctrl_StateDKType *dkst = NULL;
  
  if( (rtpl == NULL) || (rtpl->pl_len != sizeof(Ctrl_StateDKType)) )
  {
    return 1;
  }
  
  dkst = (Ctrl_StateDKType*)(rtpl->payload);
  
  memset(&jsp, 0, sizeof(Network_JsonPointerType));
  memset(valuebuf, 0, sizeof(valuebuf));
  
  jsp.property = cJSON_CreateObject();
	
  snprintf(valuebuf, 128, "%u-%u-%u-%u", dkst->tgkg[0], dkst->tgkg[1], dkst->tgkg[2],dkst->tgkg[3]);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "dktg", jsp.js_value);
  
  
  snprintf(valuebuf, 128, "%u-%u-%u-%u-%u-%u", dkst->ptkg[0], dkst->ptkg[1], dkst->ptkg[2], \
                                               dkst->ptkg[3], dkst->ptkg[4], dkst->ptkg[5]);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "dkkq", jsp.js_value);
  
  
  jsp.identity = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.identity, "productID", Route_str_pids[RT_DV_DK]);
  cJSON_AddStringToObject(jsp.identity, "deviceName", Route_DeviceList.dk[0].did);
  
  jsp.arrayitem = cJSON_CreateObject();
  cJSON_AddItemToObject(jsp.arrayitem, "identity", jsp.identity);
  cJSON_AddItemToObject(jsp.arrayitem, "properties", jsp.property);
  
  jsp.params = cJSON_CreateArray();
  cJSON_AddItemToArray(jsp.params, jsp.arrayitem);
  
  jsp.root = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.root, "id", "1");
  cJSON_AddStringToObject(jsp.root, "version", ONEJSON_VERSION);
  cJSON_AddItemToObject(jsp.root, "params", jsp.params);
  
  cJSON_PrintPreallocated(jsp.root, Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, 0);

	snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Pack_Post, ROUTE_SELF_DID);
	
  cJSON_Delete(jsp.js_value);
  cJSON_Delete(jsp.root);
  cJSON_Delete(jsp.params);
  cJSON_Delete(jsp.arrayitem);
  cJSON_Delete(jsp.identity);
  cJSON_Delete(jsp.property);
	
	return 0;
}

static uint8_t route_dk_up_para(Route_PayloadType *rtpl)
{
  Ctrl_ParaDKType *dkpara = NULL;
  
  if( (rtpl == NULL) || (rtpl->pl_len != sizeof(Ctrl_ParaDKType)) )
  {
    return 1;
  }
  
  dkpara = (Ctrl_ParaDKType*)(rtpl->payload);
  
  memset(&jsp, 0, sizeof(Network_JsonPointerType));
  memset(valuebuf, 0, sizeof(valuebuf));
  
  jsp.property = cJSON_CreateObject();

  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 128, "%u-%u-%u-%u", dkpara->tgjs[0], dkpara->tgjs[1], dkpara->tgjs[2], dkpara->tgjs[3]);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "tgdj", jsp.js_value);
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 128, "%u-%u-%u-%u-%u-%u", dkpara->dktm[0].dkkqtm1, dkpara->dktm[1].dkkqtm1, dkpara->dktm[2].dkkqtm1, \
                                               dkpara->dktm[3].dkkqtm1, dkpara->dktm[4].dkkqtm1, dkpara->dktm[5].dkkqtm1);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "dkkq1", jsp.js_value);
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 128, "%u-%u-%u-%u-%u-%u", dkpara->dktm[0].dkgbtm1, dkpara->dktm[1].dkgbtm1, dkpara->dktm[2].dkgbtm1, \
                                               dkpara->dktm[3].dkgbtm1, dkpara->dktm[4].dkgbtm1, dkpara->dktm[5].dkgbtm1);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "dkgb1", jsp.js_value);
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 128, "%u-%u-%u-%u-%u-%u", dkpara->dktm[0].dkkqtm2, dkpara->dktm[1].dkkqtm2, dkpara->dktm[2].dkkqtm2, \
                                               dkpara->dktm[3].dkkqtm2, dkpara->dktm[4].dkkqtm2, dkpara->dktm[5].dkkqtm2);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "dkkq2", jsp.js_value);
  
  jsp.js_value = cJSON_CreateObject();
  snprintf(valuebuf, 128, "%u-%u-%u-%u-%u-%u", dkpara->dktm[0].dkgbtm2, dkpara->dktm[1].dkgbtm2, dkpara->dktm[2].dkgbtm2, \
                                               dkpara->dktm[3].dkgbtm2, dkpara->dktm[4].dkgbtm2, dkpara->dktm[5].dkgbtm2);
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "dkgb2", jsp.js_value);
  
  
  jsp.identity = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.identity, "productID", Route_str_pids[RT_DV_DK]);
  cJSON_AddStringToObject(jsp.identity, "deviceName", Route_DeviceList.dk[0].did);
  
  jsp.arrayitem = cJSON_CreateObject();
  cJSON_AddItemToObject(jsp.arrayitem, "identity", jsp.identity);
  cJSON_AddItemToObject(jsp.arrayitem, "properties", jsp.property);
  
  jsp.params = cJSON_CreateArray();
  cJSON_AddItemToArray(jsp.params, jsp.arrayitem);
  
  jsp.root = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.root, "id", "1");
  cJSON_AddStringToObject(jsp.root, "version", ONEJSON_VERSION);
  cJSON_AddItemToObject(jsp.root, "params", jsp.params);
  
  cJSON_PrintPreallocated(jsp.root, Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, 0);

	snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Pack_Post, ROUTE_SELF_DID);
	
  cJSON_Delete(jsp.js_value);
  cJSON_Delete(jsp.root);
  cJSON_Delete(jsp.params);
  cJSON_Delete(jsp.arrayitem);
  cJSON_Delete(jsp.identity);
  cJSON_Delete(jsp.property);
  
  return 0;
}

uint8_t Route_dk_up(Route_PayloadType *rtpl)
{
  uint8_t err = 1;
  
  switch(rtpl->pl_type)
  {
		case ROUTE_PLTYP_DK_STATE:
			err = route_dk_up_state(rtpl);
			break;
    
    case ROUTE_PLTYP_DK_PARA:
			err = route_dk_up_para(rtpl);
			break;
    
    default:
      err = 1;
      break;
  }
  
  return err;
}
